// Menu.cpp: implementation of the Menu class.
//
//////////////////////////////////////////////////////////////////////

#include "Menu.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Menu::Menu(float x, float y, int col, float icon_w, float icon_h)
{
this->start_x = x;
this->start_y = y;

this->cols = col;

this->icon_w = icon_w;
this->icon_h = icon_h;

this->selected = false;
this->show     = true;

this->menuEntries = new ulLinkedList();
this->numMenuEntries = 0;

old_sel = -1;
curr_sel = -1;
}

Menu::~Menu()
{

}

int Menu::passive(int button, int state, int x, int y)
{
int over = 0;

if(show)
	{
	for(int i = 0; i < numMenuEntries; i++)
		{
		MenuEntry *me = (MenuEntry*)menuEntries->getNodeData(i);
		over += me->button->mouse(button,state,x,y);
		if(me->submenu != NULL) over += me->submenu->mouse(button,state,x,y);
		}
	}
if(over > 0) this->focused = true;
else this->focused = false;
return focused;
}

int Menu::mouse(int button, int state, int x, int y)
{
int over = 0;

if(show)
	{
	for(int i = 0; i < numMenuEntries; i++)
		{
		MenuEntry *me = (MenuEntry*)menuEntries->getNodeData(i);
		over += me->button->mouse(button,state,x,y);
		if(me->submenu != NULL)
				{	
				over += me->submenu->mouse(button,state,x,y);
				if(me->submenu->isSelected()) me->submenu->hide();
				}
		if(me->button->selected()) 
			{
			if( (old_sel != i) && (old_sel != -1) )
				{
				MenuEntry *me2 = (MenuEntry*)menuEntries->getNodeData(old_sel);
				me2->button->change();
				if(me2->submenu != NULL) me2->submenu->hide();
				}
			if(me->submenu != NULL) /*if(old_sel != i)*/ if(button == GLUT_RIGHT_BUTTON) me->submenu->reveal();
			curr_sel = i;
			}
		else if(old_sel == i)
			{
			if(me->submenu != NULL) me->submenu->hide();
			curr_sel = -1;
			}
		}
	}
old_sel = curr_sel;

if(over > 0) this->focused = true;
else this->focused = false;
return focused;
}

bool Menu::isFocused()
{
return focused;
}

void Menu::display()
{
if(show)
	{
	for(int i = 0; i < numMenuEntries; i++)
		{
		MenuEntry *me = (MenuEntry*)menuEntries->getNodeData(i);
		me->button->display();
		if(me->submenu != NULL) me->submenu->display();
		}
	}
}

void Menu::reveal()
{
this->show = true;
}

void Menu::hide()
{
this->show = false;
}


bool Menu::isVisible()
{
return show;
}

int Menu::getMenuEntry()
{
int sel_entry = -1;

for(int i = 0; i < numMenuEntries; i++)
	{
	MenuEntry *me = (MenuEntry*)menuEntries->getNodeData(i);
	if(me->submenu != NULL) if(me->submenu->isSelected()) sel_entry = i;
	}
return curr_sel;
}

int Menu::getSubMenuEntry()
{
int sel_entry = -1;

if(curr_sel != -1)
	{
	MenuEntry *me = (MenuEntry*)menuEntries->getNodeData(curr_sel);
	if(me->submenu != NULL) sel_entry = me->submenu->getSelectedEntry();
	}
return sel_entry;
}


void Menu::add(MenuEntry *me)
{
me->button->set(start_x + (float)(numMenuEntries%cols)*icon_w, start_y - (float)(numMenuEntries/cols)*icon_h,icon_w,icon_h);
//printf("%f %f %f %f\n",start_x + (float)(numMenuEntries%cols)*icon_w, start_y - (float)(numMenuEntries/cols)*icon_h,icon_w,icon_h);
this->menuEntries->appendNode(me);
this->numMenuEntries++;
}

void Menu::bindTextures()
{
//printf("numMenuEntries: %d\n",numMenuEntries);

for(int i = 0; i < numMenuEntries; i++)
	{
	MenuEntry *me = (MenuEntry*)menuEntries->getNodeData(i);
	if(me->submenu != NULL) me->submenu->bindTextures();
	me->button->bindTexture();
	}
}
